package org.dromara.comm.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.dromara.comm.domain.AsCircleManagement;
import org.dromara.comm.domain.AsLifecircleClassify;
import org.dromara.comm.mapper.AsCircleManagementMapper;
import org.dromara.comm.mapper.AsLifecircleClassifyMapper;
import org.dromara.comm.service.AsCircleManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 生活圈分类业务层实现
 * 负责处理与生活圈分类相关的业务逻辑
 *
 * @author: 郭伟明
 * @date: 2024-08-01 10:36
 */
@Service
public class AsCircleManagementServiceImpl extends ServiceImpl<AsCircleManagementMapper, AsCircleManagement> implements AsCircleManagementService {

    @Autowired
    private AsCircleManagementMapper asCircleManagementMapper;

    @Autowired
    private AsLifecircleClassifyMapper asLifecircleClassifyMapper;

    /**
     * 查询生活圈分类及其对应的生活圈信息
     * 该方法首先获取所有生活圈分类，然后根据每个分类查询对应的生活圈信息，并将信息按分类整理
     *
     * @return 包含生活圈分类及其信息的Map对象
     */
    @Override
    public Map<String, Object> selectAsCircleManagement() {
        Map<String, Object> resultMap = new HashMap<>();
        // 存储最终分类名称及其对应生活圈信息的列表
        List<Map<String, Object>> classifyNamesList = new ArrayList<>();
        // 获取所有生活圈分类
        List<AsLifecircleClassify> classifyList = asLifecircleClassifyMapper.selectList(null);
        // 用于临时存储每个分类及其对应生活圈信息的Map
        Map<String, List<Map<String, Object>>> classifyMap = new HashMap<>();
        // 初始化分类Map，以每个分类名称为键
        for (AsLifecircleClassify classify : classifyList) {
            classifyMap.put(classify.getClassifyName(), new ArrayList<>());
        }
        // 获取所有生活圈信息
        List<AsCircleManagement> circleManagementList = asCircleManagementMapper.selectList(null);
        // 根据生活圈的分类ID，将生活圈信息添加到对应的分类中
        for (AsCircleManagement circle : circleManagementList) {
            AsLifecircleClassify classify = asLifecircleClassifyMapper.selectById(circle.getClassId());
            if (classify != null) {
                List<Map<String, Object>> circleManagementDataList = classifyMap.get(classify.getClassifyName());
                if (circleManagementDataList != null) {
                    Map<String, Object> circleData = new HashMap<>();
                    circleData.put("id", circle.getId());
                    circleData.put("introduce", circle.getIntroduce());
                    circleData.put("classId", circle.getClassId());
                    circleManagementDataList.add(circleData);
                }
            }
        }
        // 将分类及其生活圈信息整理成最终的格式
        for (Map.Entry<String, List<Map<String, Object>>> entry : classifyMap.entrySet()) {
            Map<String, Object> classifyDataMap = new HashMap<>();
            classifyDataMap.put("classifyName", entry.getKey());
            classifyDataMap.put("asCircleManagement", entry.getValue());
            classifyNamesList.add(classifyDataMap);
        }
        // 对分类进行排序，以满足特定的顺序要求
        classifyNamesList.sort((a, b) -> {
            List<String> desiredOrder = Arrays.asList("休闲", "餐饮", "玩乐", "教培", "AED");
            String nameA = (String) a.get("classifyName");
            String nameB = (String) b.get("classifyName");
            return Integer.compare(desiredOrder.indexOf(nameA), desiredOrder.indexOf(nameB));
        });
        // 将整理好的分类及其生活圈信息列表返回
        resultMap.put("classifyNames", classifyNamesList);
        return resultMap;
    }

}
